#include "../OglApp.h"
#include "../Mesh.h"
#include "../Vertex.h"
#include "../Shader.h"
#include "../SimpleFrame.h"
#include "../ShaderMng.h"

Shader shader;
Gut::MeshIndice mesh;
SimpleFrame debugFrame;

namespace Gut
{
    struct Vec3
    {
        float x, y, z;
    };
    struct Vec2
    {
        float x, y;
    };

    struct Vec4
    {
        float x, y, z, w;
    };
} // namespace Gut

struct VertexPos
{
    Gut::Vec3 pos;
};

const std::vector<VertexLayout> inputLayout =
    {
        {"POSITION", 0, 3, GL_FLOAT, GL_FALSE, offsetof(VertexPos, pos)},
};
struct MeshApp : public OglApp
{
    ShaderMng shaderMng;
    
    MeshApp()
        : OglApp()
    {
    }

    bool Create(const GLFWConfig &config) override
    {
        if (!OglApp::Create(config))
        {
            return false;
        }
        LOGI("create window successfully");

        std::vector<VertexPos> vertices =
            {
                {0.5f, 0.5f, 0.0f},
                {0.5f, -0.5f, 0.0f},
                {-0.5f, 0.0f, 0.0f},
            };
        std::vector<unsigned int> indices =
            {0, 1, 2};
        mesh.CreateVertexIndiceInputLayout<VertexPos, unsigned int>(vertices, indices, inputLayout);
       
        LOGI("create mesh");
        shaderMng.mDir = "shader/";
        shader = shaderMng.CreatePath("edit.vert", "edit.frag");

        // shader.CreateFromFilePath("shader/edit.vert", "shader/edit.frag");
        LOGI("Create shader");

        SimpleFrame::Desc desc;
        desc.width = 800;
        desc.height = 600;
        debugFrame.Create(desc);
        LOGI("Create SimpleFrame");
        return true;
    }
    void OnFrameRender() override
    {
        OglApp::OnFrameRender();

        debugFrame.Begin();
        shader.Use();
        mesh.Draw();
        debugFrame.End();
        
    }
};

int main()
{
    GLFWConfig config;

    MeshApp app;
    if (!app.Create(config))
    {
        LOGE("Cannot create the window ");
        return 0;
    }

    app.Execute();
    app.OnClose();
    return 0;
}
